<template>
  <div class="security-settings">
    <h3 class="section-title">账户安全设置</h3>
    <p class="section-desc">保护您的账号安全，定期更新安全信息</p>
    
    <div class="security-items">
      <!-- 登录密码 -->
      <div class="security-item">
        <div class="item-icon secure">
          <icon-lock />
        </div>
        <div class="item-content">
          <div class="item-title">
            <span>登录密码</span>
            <span class="status-tag secure">已设置</span>
          </div>
          <div class="item-desc">
            密码至少6位字符，支持数字、字母和除空格外的特殊字符，且必须同时包含数字和大小写字母。
          </div>
        </div>
        <div class="item-action">
          <a-button size="medium" @click="showMaintenanceMessage">修改</a-button>
        </div>
      </div>
      
      <!-- 密保问题 -->
      <div class="security-item">
        <div class="item-icon warning">
          <icon-question-circle />
        </div>
        <div class="item-content">
          <div class="item-title">
            <span>密保问题</span>
            <span class="status-tag warning">未设置</span>
          </div>
          <div class="item-desc">
            密保问题可以有效保护账号的安全，建议您尽快设置密保问题。
          </div>
        </div>
        <div class="item-action">
          <a-button type="outline" status="warning" size="medium" @click="showMaintenanceMessage('密保问题')">设置</a-button>
        </div>
      </div>
      
      <!-- 安全手机 -->
      <div class="security-item">
        <div class="item-icon secure">
          <icon-mobile />
        </div>
        <div class="item-content">
          <div class="item-title">
            <span>安全手机</span>
            <span class="status-tag secure">已绑定</span>
          </div>
          <div class="item-desc">
            已绑定：{{ formatPhone(userStore.mobile) || '未设置手机号' }}
          </div>
        </div>
        <div class="item-action">
          <a-button size="medium" @click="showMaintenanceMessage('手机号')">修改</a-button>
        </div>
      </div>
      
      <!-- 安全邮箱 -->
      <div class="security-item">
        <div class="item-icon" :class="{ 'secure': userStore.email, 'warning': !userStore.email }">
          <icon-email />
        </div>
        <div class="item-content">
          <div class="item-title">
            <span>安全邮箱</span>
            <span class="status-tag" :class="{ 'secure': userStore.email, 'warning': !userStore.email }">
              {{ userStore.email ? '已绑定' : '未设置' }}
            </span>
          </div>
          <div class="item-desc">
            <template v-if="userStore.email">
              已绑定：{{ maskEmail(userStore.email) }}
            </template>
            <template v-else>
              绑定邮箱可以用来找回密码、接收通知等，建议您尽快设置。
            </template>
          </div>
        </div>
        <div class="item-action">
          <a-button 
            :type="userStore.email ? 'secondary' : 'outline'" 
            :status="userStore.email ? 'normal' : 'warning'" 
            size="medium"
            @click="showMaintenanceMessage('邮箱')"
          >
            {{ userStore.email ? '修改' : '设置' }}
          </a-button>
        </div>
      </div>
      
      <!-- 分隔线 -->
      <div class="divider">
        <span>高级设置</span>
      </div>
      
      <!-- 账号注销 -->
      <div class="security-item danger">
        <div class="item-icon danger">
          <icon-delete />
        </div>
        <div class="item-content">
          <div class="item-title">
            <span>账号注销</span>
          </div>
          <div class="item-desc">
            注销账号将删除您的所有数据，此操作不可逆，请谨慎操作。
          </div>
        </div>
        <div class="item-action">
          <a-button type="outline" status="danger" size="medium" @click="showMaintenanceMessage('账号注销', 'error')">申请注销</a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useUserStore } from '@/store';
import { onMounted } from 'vue';
import { SysUserService } from '@/api/gen-api';
import { Message } from '@arco-design/web-vue';

const userStore = useUserStore();

// 显示系统维护提示信息
const showMaintenanceMessage = (feature = '密码', type = 'warning') => {
  const messages = {
    warning: `系统维护中，暂不能修改${feature}`,
    error: `系统维护中，暂不支持${feature}功能`,
    info: `${feature}功能正在升级中，请稍后再试`
  };
  
  Message[type]({
    content: messages[type],
    duration: 3000,
    closable: true
  });
};

// 格式化电话号码，保留前三位和后四位，中间用星号代替
const formatPhone = (phone: string | undefined): string => {
  if (!phone || phone.length < 7) return phone || '';
  return `${phone.substring(0, 3)}****${phone.substring(phone.length - 4)}`;
};

// 格式化邮箱，保留前两位和@后面的内容
const maskEmail = (email: string | undefined): string => {
  if (!email || !email.includes('@')) return email || '';
  const [username, domain] = email.split('@');
  if (username.length <= 2) return email;
  return `${username.substring(0, 2)}****@${domain}`;
};

onMounted(() => {
  // 确保获取最新用户信息
  SysUserService.getInfo().then(res => {
    if (res.result) {
      userStore.setInfo(res.result);
    }
  });
});
</script>

<style scoped lang="less">
.security-settings {
  padding: 24px;
  background-color: var(--color-bg-2);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.section-title {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--color-text-1);
}

.section-desc {
  margin: 0 0 28px 0;
  font-size: 14px;
  color: var(--color-text-3);
  }

.security-items {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.security-item {
  display: flex;
  align-items: center;
  padding: 20px;
  background-color: var(--color-fill-1);
  border-radius: 8px;
  transition: all 0.3s ease;
  position: relative;
  
  &:hover {
    background-color: var(--color-fill-2);
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  }

  &.danger {
    background-color: rgb(var(--danger-1));
    
    &:hover {
      background-color: rgb(var(--danger-2));
    }
  }
}

.item-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  margin-right: 20px;
  font-size: 24px;
  
  &.secure {
    color: #2759CD;
    background-color: rgba(39, 89, 205, 0.1);
  }
  
  &.warning {
    color: rgb(var(--warning-6));
    background-color: rgb(var(--warning-1));
  }
  
  &.danger {
    color: rgb(var(--danger-6));
    background-color: rgb(var(--danger-1));
  }
}

.item-content {
  flex: 1;
  min-width: 0;

  .item-title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-1);
    margin-bottom: 8px;

    .status-tag {
      margin-left: 12px;
      font-size: 12px;
      font-weight: normal;
      padding: 2px 8px;
      border-radius: 10px;
      
      &.secure {
        color: #2759CD;
        background-color: rgba(39, 89, 205, 0.1);
      }
      
      &.warning {
        color: rgb(var(--warning-6));
        background-color: rgb(var(--warning-1));
      }
    }
  }
  
  .item-desc {
    font-size: 14px;
    color: var(--color-text-3);
    line-height: 1.6;
    max-width: 600px;
  }
}

.item-action {
  margin-left: 24px;
  
  .arco-btn {
    min-width: 80px;
    font-weight: 500;
    transition: all 0.3s ease;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }
  }
}

.divider {
  display: flex;
  align-items: center;
  color: var(--color-text-3);
  font-size: 14px;
  margin: 16px 0 8px;
  
  &::before,
  &::after {
    content: '';
    flex: 1;
    height: 1px;
    background-color: var(--color-border-2);
    }

  span {
    padding: 0 12px;
  }
}

// 移动端适配
@media (max-width: 768px) {
  .security-settings {
    padding: 20px;
  }
  
  .security-item {
    flex-direction: column;
    align-items: flex-start;
    
    .item-icon {
      margin-right: 0;
      margin-bottom: 16px;
    }
    
    .item-content {
      width: 100%;
      margin-bottom: 20px;
      
      .item-title {
        flex-wrap: wrap;
        
        .status-tag {
          margin-left: 8px;
        }
      }
    }
    
    .item-action {
      width: 100%;
      margin-left: 0;
      
      .arco-btn {
        width: 100%;
      }
    }
  }
}
</style>
